var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index_array:[0],
    array: ['请选择滤芯配件', 'pp棉滤芯', 'PR反渗透滤芯', '前置活性炭滤芯','RO膜'],
    eq_num:'',
    eq_info:[],
    title:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.setData({
        eq_num:options.eq_sn
      })
      this.getEq();
  },
  getEq() {
    var that = this;
    if (that.data.eq_num == '') {
      app.showToast('请输入设备号');
      return false;
    }
    app.ajax({
      url: "Manage/Equipment/get_equipment",
      data: {
        eq_sn: that.data.eq_num,
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            eq_info: res.data.data
          })
        }
      }

    })
  },
  
  //选中的值
  bindPickerChange: function (e) {
    var that = this;
    var choosed = e.detail.value;
    var indno = e.currentTarget.dataset.indnum;
    var changeArray = that.data.index_array;
    var title = that.data.title;

    changeArray[indno] = choosed;
    console.log(indno);

    title[indno] = that.data.array[changeArray[indno]]; 
    that.setData({
      index_array: changeArray,
      title: title
    })
    console.log(title);
  },
  //增加记录
  addChange:function(e){
    var that = this;
    var currNum = e.currentTarget.dataset.indnum;
    var changeItem = that.data.index_array;

    changeItem = changeItem.concat(0);
    that.setData({
      index_array: changeItem,
    })
  },
  //减少
  cutChange:function(e){
    var that = this;
    var cutNum = e.currentTarget.dataset.indnum;
    console.log(cutNum)
    var cutItem = that.data.index_array;
    //删除
    var title = that.data.title;
    title.splice(cutNum,1);
    console.log(title)

    if (cutItem.length <= 1) {
      return false;
    } else {
      cutItem.splice(cutNum, 1);
    }
    that.setData({
      index_array: cutItem
    })
  },  
  //跳转
  facilityMsg:function(){

    var that = this;
    if (that.data.eq_num == '') {
      app.showToast('请输入设备号');
      return false;
    }
    var title = that.data.title;
    if (title.length<=0){
      app.showToast('请选择配件类型');return false;
    }
    for(var i=0;i<title.length;i++){
      console.log(title[i])
      if(title[i] == undefined || title[i]=="请选择滤芯配件"){
        app.showToast('请选择滤芯配件');return false;
      }
    }
    
    app.ajax({
      url: "Manage/Equipment/addChangLog",
      data: {
        sn: that.data.eq_num,
        title:that.data.title,
      },
      success: function (res) {
        if (res.data.code == 200) {
          app.showToast(res.data.message);
          wx.navigateBack({
            delta:1
          })
        }else{
          app.showToast(res.data.message);
        }
      }
    })
  }
})